﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RapidWebDev.SearchIndex.Contract
{
	/// <summary>
	/// The implemention to create searching highlight fragment.
	/// </summary>
	public class SearchHighlighter : ISearchHighlighter
	{
		/// <summary>
		/// Markup indicates where the highlight fragment starts.
		/// </summary>
		public const string StartHighlightMarkup = "{___highlight___}";

		/// <summary>
		/// Markup indicates where the highlight fragment ends.
		/// </summary>
		public const string EndHighlightMarkup = "{___/highlight___}";

		/// <summary>
		/// Get start text for highlight fragment.
		/// </summary>
		public string StartHighlightText { get; private set; }

		/// <summary>
		/// Get end text for highlight fragment.
		/// </summary>
		public string EndHighlightText { get; private set; }

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="startHighlightText">Start text for highlight fragment.</param>
		/// <param name="endHighlightText">End text for highlight fragment.</param>
		public SearchHighlighter(string startHighlightText, string endHighlightText)
		{
			this.StartHighlightText = startHighlightText;
			this.EndHighlightText = endHighlightText;
		}

		/// <summary>
		/// Resolve highlight fragment for UI rendering.
		/// </summary>
		/// <param name="s"></param>
		/// <returns></returns>
		public string ResolveHighlightFragment(string s)
		{
			if (s == null) return s;
			return s.Replace(StartHighlightMarkup, this.StartHighlightText)
				.Replace(EndHighlightMarkup, this.EndHighlightText);
		}

		/// <summary>
		/// Does any string in the parameter contain highlight fragment.
		/// </summary>
		/// <param name="sArray"></param>
		/// <returns></returns>
		public bool ContainsHighlightFragment(params string[] sArray)
		{
			if (sArray == null || sArray.Length == 0) return false;
			foreach (string s in sArray)
			{
				if (string.IsNullOrEmpty(s)) continue;
				if (s.Contains(StartHighlightMarkup)) return true;
			}

			return false;
		}
	}
}
